﻿using Abp.Linq.Expressions;
using Common.Enums;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using utils.enums;
using utils.Enums;
using utils.EX;
using Utils;

namespace BLL
{
    /// <summary>
    /// 耗材分类BLL层
    /// </summary>
    public class CategoryBLL : ICategoryBLL
    {
        /// <summary>
        /// 耗材分类DAL接口
        /// </summary>
        ICategoryDAL _categoryDal;

        /// <summary>
        /// 用户接口
        /// </summary>
        IUserDAL _userDAL;

        public CategoryBLL(ICategoryDAL category, IUserDAL userDAL)
        {
            _categoryDal = category;
            _userDAL = userDAL;
        }

        /// <summary>
        /// 耗材分类数据列表、搜索
        /// </summary>
        /// <param name="categoryName">耗材分类名称</param>
        /// <param name="page">页数</param>
        /// <param name="limit">数据条数</param>
        /// <returns></returns>
        public List<CategoryDTO> getcategoryList(string categoryName, string page, string limit, out int cont)
        {
            //连表
            IQueryable<Category> datas = _categoryDal.getEntitys();

            if (!string.IsNullOrEmpty(categoryName))
            {
                datas = datas.Where(i => i.CategoryName.Contains(categoryName));
            }
            //分页
            var pagecategory = datas.Skip((int.Parse(page) - 1) * int.Parse(limit)).Take(int.Parse(limit)).ToList();
            cont = datas.Count();

            //整合数据
            List<CategoryDTO> categoryList = new List<CategoryDTO>();
            foreach (var item in pagecategory)
            {
                categoryList.Add(new CategoryDTO(item));
            }
            return categoryList;
        }

        /// <summary>
        /// 添加耗材分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>。
        public bool AddCategory(Category category)
        {
            category.Id = Guid.NewGuid().ToString();
            //根据账号查询用户信息
            Category bycategoryInfo = _categoryDal.getEntitys().FirstOrDefault(d =>  d.CategoryName == category.CategoryName);
            if (bycategoryInfo == null)
            {
                //保存
                if (_categoryDal.AddEntity(category) < 0)
                {
                    throw new MyException(((int)UserEnums.USER_STATE.LOSE), Enums.getDescribe(UserEnums.USER_STATE.LOSE));
                }
            }
            else
            {
                throw new MyException(((int)CategoryEnums.STATE.ALREADY), Enums.getDescribe(CategoryEnums.STATE.ALREADY));
            }
            return true;
        }

        /// <summary>
        /// 删除耗材分类
        /// </summary>
        /// <param name="categoryId">耗材分类id</param>
        /// <returns></returns>
        public bool DalateCategory(string categoryId)
        {
            //删除
            if (_categoryDal.DalateEntity(categoryId) < 0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.DATATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.DATATE_LOSE));
            }
            return true;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="categoryIds"></param>
        /// <returns></returns>
        public bool DalateCategoryList(List<String> categoryIds)
        {
            if (categoryIds.Count < 1)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LIST), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LIST));
            }
            foreach (var item in categoryIds)
            {
                DalateCategory(item);
            }
            return true;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public bool UpdataCategory(Category category)
        {
            if (_categoryDal.UpdataEntity(category) < 0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LOSE));
            }
            return true;
        }

        /// <summary>
        /// 根据耗材分类id搜索
        /// </summary>
        /// <param name="categoryid"></param>
        /// <returns></returns>
        public Category getByCategory(string categoryid)
        {
            return _categoryDal.getEntitys().FirstOrDefault(c => c.Id.EndsWith(categoryid));
        }


        /// <summary>
        /// 耗材分类下拉框
        /// </summary>
        /// <returns></returns>
        public List<CategoryDTO> getCategorySelect()
        {
            return _categoryDal.getEntitys().Select(c => new CategoryDTO(c)).ToList();
        }


    }
}
